package com.yc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.yc.exception.CustomException;
import com.yc.mapper.FeedbackMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.yc.entity.LeavereQuests;
import com.yc.mapper.LeavereQuestsMapper;
import com.yc.service.LeavereQuestsService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 请假表;(leavere_quests)表服务实现类
 * @date : 2023-6-28
 */
@Service
public class LeavereQuestsServiceImpl implements LeavereQuestsService{
    @Autowired
    private LeavereQuestsMapper leavereQuestsMapper;
    @Autowired
    private FeedbackMapper feedbackMapper;

    public void Deletebyid(int id){
        leavereQuestsMapper.Deletebyid(id);
    }

    /**
     * 查询是否有冲突的请假记录
     * @param stuid 学生id
     * @param start 申请的开始时间
     * @param end 申请的结束时间
     * @return 冲突的请假记录
     */
    public List<Map<String,Object>> startandendbystuid(String stuid,String start,String end){
       List<Map<String,Object>> ithave = leavereQuestsMapper.startandendbystuid(stuid);
        Map<String,Object> jl = new HashMap<>();
        List<Map<String,Object>> jllist = new ArrayList<>();
        for (Map<String, Object> map : ithave) {
            String start1 = (String) map.get("request_start");
            String end1 = (String) map.get("request_end");
            int startcompare1 = start.compareTo(start1);
            int startcompare2 = start.compareTo(end1);
            if (startcompare2>0){
                for (Map<String, Object> map2 : ithave) {
                    String start2 = (String) map2.get("request_start");
                    String end2 = (String) map2.get("request_end");
                    int endcompare1 = start2.compareTo(end);
                    int startcompare3 = start2.compareTo(start);
                    int endcompare2 = end2.compareTo(end);
                    if (startcompare3>0&&endcompare1<0&&endcompare2>0){
                        int id = (int) map2.get("id");
                        jl =  leavereQuestsMapper.selectbyid(id);
                        jllist.add(jl);
                    } else if(startcompare3>0&&endcompare2<0){
                        int id = (int) map2.get("id");
                        jl =  leavereQuestsMapper.selectbyid(id);
                        jllist.add(jl);
                    }
                }
            } else if (startcompare1>0&&startcompare2<0){
                int id = (int) map.get("id");
                jl =  leavereQuestsMapper.selectbyid(id);
                jllist.add(jl);
                for (Map<String, Object> map2 : ithave) {
                    String start2 = (String) map2.get("request_start");
                    String end2 = (String) map2.get("request_end");
                    int endcompare1 = start2.compareTo(end);
                    int startcompare3 = start2.compareTo(start);
                    int endcompare2 = end2.compareTo(end);
                    if (startcompare3>0&&endcompare1<0&&endcompare2>0){
                        id = (int) map2.get("id");
                        jl =  leavereQuestsMapper.selectbyid(id);
                        jllist.add(jl);
                    } else if(startcompare3>0&&endcompare2<0){
                        id = (int) map2.get("id");
                        jl =  leavereQuestsMapper.selectbyid(id);
                        jllist.add(jl);
                    }
                }

            } else if (startcompare1==0){
                int id = (int) map.get("id");
                jl =  leavereQuestsMapper.selectbyid(id);
                jllist.add(jl);
                for (Map<String, Object> map2 : ithave) {
                    String start2 = (String) map2.get("request_start");
                    String end2 = (String) map2.get("request_end");
                    int endcompare1 = start2.compareTo(end);
                    int startcompare3 = start2.compareTo(start);
                    int endcompare2 = end2.compareTo(end);
                    if (startcompare3>0&&endcompare1<0&&endcompare2>0){
                        id = (int) map2.get("id");
                        jl =  leavereQuestsMapper.selectbyid(id);
                        jllist.add(jl);
                    } else if(startcompare3>0&&endcompare2<0){
                        id = (int) map2.get("id");
                        jl =  leavereQuestsMapper.selectbyid(id);
                        jllist.add(jl);
                    }
                }
            }
        }
        return jllist;
    }

    /**
     *  添加请假申请数据
     * @param stuid 学生id
     * @param starttime 开始时间
     * @param overtime 结束时间
     * @param reason 请假原因
     * @param filepath 请假条照片
     */
    public void insertrequest(String stuid, String starttime, String overtime, String reason, String filepath){
        String id = feedbackMapper.selectidbystuid(stuid);
        leavereQuestsMapper.insertrequest(stuid,starttime,overtime,filepath,reason,id);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public LeavereQuests queryById(Integer id){
        return leavereQuestsMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param leavereQuests 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    @Override
    public Page<LeavereQuests> paginQuery(LeavereQuests leavereQuests, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<LeavereQuests> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(leavereQuests.getStuid())){
            queryWrapper.eq(LeavereQuests::getStuid, leavereQuests.getStuid());
        }
        if(StrUtil.isNotBlank(leavereQuests.getRequestStart())){
            queryWrapper.eq(LeavereQuests::getRequestStart, leavereQuests.getRequestStart());
        }
        if(StrUtil.isNotBlank(leavereQuests.getRequestEnd())){
            queryWrapper.eq(LeavereQuests::getRequestEnd, leavereQuests.getRequestEnd());
        }
        if(StrUtil.isNotBlank(leavereQuests.getImagepath())){
            queryWrapper.eq(LeavereQuests::getImagepath, leavereQuests.getImagepath());
        }
        if(StrUtil.isNotBlank(leavereQuests.getReason())){
            queryWrapper.eq(LeavereQuests::getReason, leavereQuests.getReason());
        }
        //2. 执行分页查询
        Page<LeavereQuests> pagin = new Page<>(current , size , true);
        IPage<LeavereQuests> selectResult = leavereQuestsMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param leavereQuests 实例对象
     * @return 实例对象
     */
    @Override
    public LeavereQuests insert(LeavereQuests leavereQuests){
        leavereQuestsMapper.insert(leavereQuests);
        return leavereQuests;
    }

    /**
     * 更新数据
     *
     * @param leavereQuests 实例对象
     * @return 实例对象
     */
    @Override
    public LeavereQuests update(LeavereQuests leavereQuests){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<LeavereQuests> chainWrapper = new LambdaUpdateChainWrapper<LeavereQuests>(leavereQuestsMapper);
        if(StrUtil.isNotBlank(leavereQuests.getStuid())){
            chainWrapper.eq(LeavereQuests::getStuid, leavereQuests.getStuid());
        }
        if(StrUtil.isNotBlank(leavereQuests.getRequestStart())){
            chainWrapper.eq(LeavereQuests::getRequestStart, leavereQuests.getRequestStart());
        }
        if(StrUtil.isNotBlank(leavereQuests.getRequestEnd())){
            chainWrapper.eq(LeavereQuests::getRequestEnd, leavereQuests.getRequestEnd());
        }
        if(StrUtil.isNotBlank(leavereQuests.getImagepath())){
            chainWrapper.eq(LeavereQuests::getImagepath, leavereQuests.getImagepath());
        }
        if(StrUtil.isNotBlank(leavereQuests.getReason())){
            chainWrapper.eq(LeavereQuests::getReason, leavereQuests.getReason());
        }
        //2. 设置主键，并更新
        chainWrapper.set(LeavereQuests::getId, leavereQuests.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(leavereQuests.getId());
        }else{
            return leavereQuests;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id){
        int total = leavereQuestsMapper.deleteById(id);
        return total > 0;
    }

    /**
     * 通过请假表Id查询请假信息
     * @param leaveId
     * @return
     */
    public LeavereQuests selectByLeaveId(@Param("leaveId") String leaveId){
        return leavereQuestsMapper.selectByLeaveId(leaveId);
    }

    /**
     * 通过学号和当前时间查询请假信息
     * @param stuid 学生学号
     * @return 请假信息
     */
    public LeavereQuests selectByStuid(@Param("stuid") String stuid){
        LeavereQuests leavereQuests = leavereQuestsMapper.selectByStuid(stuid);
        if (leavereQuests == null){
            throw new CustomException("0", "该学生无请假信息!");
        }
        return leavereQuests;
    }
}
